package com.tecode.myBooks.listener;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Created with Intellij IDEA.
 *
 * @Author: Gabriel
 * @Date: 2018-12-12
 * @Description:
 * 监听ServletContext对象的创建和销毁的监听器
 * 让当前类可以监听ServletContext对象的创建和销毁 那么需要实现。
 *
 * 编写好监听器后，需要把监听器注册到Web服务器中.
 *  ServletContext对象在当前工程被服务器加载时创建。
 *  ServletContext对象在当前工厂被服务器卸载时销毁。
 *
 *  可以把多个回话使用相同数据的信息放置ServletContext域中。
 *
  * @Author Gabriel
  * @Description //TODO
  * @Date
  * @Param
  * @Return
 *
 * 域对象的监听
 *  ServletContextListener   --   ServletContext
 *  HttpSessionListener   -- HttpSession
 *  ServletRequestListener   - Request
 *
 *  域对象中属性的监听
 *  	ServletContextAttributeListener,
 *  	HttpSessionAttributeListener,
 *  	ServletRequestAttributeListener
 *
  * @WebListener  使用注解的方式来吧监听器注册到web服务器中。
 *    注册监听器到web服务器
		<listener>
				<listener-class>com.tecode.myBooks.listener.ServletContextListener</listener-class>
		</listener>
 *
 */

public class ServletContextListener implements javax.servlet.ServletContextListener
,HttpSessionListener,ServletRequestListener,ServletContextAttributeListener,HttpSessionAttributeListener,ServletRequestAttributeListener{

	/**
	 * @Author Gabriel
	 * @Description //TODO
	 * @Date 2018/12/12
	 * @Param 
	 * @Return
	 *
	 * 服务器加载当前工厂时，创建ServletContext对象时调用的方法。
	 */
	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		System.out.println("ServletContextListener。。。。contextInitialized。。。");
		//可以通过创建ServletContext对象的事件对象中来获得创建的这一个ServletContext对象。
		ServletContext servletContext = servletContextEvent.getServletContext();
		servletContext.setAttribute("abc","321");
	}
	/**
 * @Author Gabriel
 * @Description //TODO
 * @Date 2018/12/12
 * @Param 
 * @Return 
	@Override
	*/
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		System.out.println("ServletContextListener。。。。contextDestroyed。。。");
		ServletContext servletContext = servletContextEvent.getServletContext();

	}


	//session对象的监听
	@Override
	public void sessionCreated(HttpSessionEvent httpSessionEvent) {
		System.out.println("session创建了");
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
		System.out.println("session销毁了");
	}


	//request兑现高度监听
	@Override
	public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
		System.out.println("requ销毁了");
	}

	@Override
	public void requestInitialized(ServletRequestEvent servletRequestEvent) {
		System.out.println("requ创建了");
		ServletRequest servletRequest = servletRequestEvent.getServletRequest();
		HttpServletRequest request =(HttpServletRequest)servletRequest;

		System.out.println("####" + request.getRequestURI());

	}


	//ServletContext属性的监听
	@Override
	public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
				System.out.println("attributeAdded.....");
		String name = servletContextAttributeEvent.getName();
		Object value = servletContextAttributeEvent.getValue();

		System.out.println(name +";" + value);
	}

	@Override
	public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
				System.out.println("attributeRemoved.....");
		String name = servletContextAttributeEvent.getName();
		Object value = servletContextAttributeEvent.getValue();

		System.out.println(name +";" + value);
	}

	@Override
	public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
				System.out.println("attributeReplaced.....");
		String name = servletContextAttributeEvent.getName();
		Object value = servletContextAttributeEvent.getValue();

		System.out.println(name +";" + value);
	}


	//session属性的监听
	@Override
	public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {

	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {

	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

	}


	//request的属性的监听
	@Override
	public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {

	}

	@Override
	public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {

	}

	@Override
	public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {

	}
}